<template>
  <div class="business-analysis">
    <!-- 筛选条件 -->
    <el-form :inline="true" :model="filterForm" class="filter-form">
      <el-form-item label="统计时间">
        <el-date-picker
          v-model="filterForm.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          :shortcuts="dateShortcuts"
        />
      </el-form-item>
      <el-form-item label="培训类型">
        <el-select v-model="filterForm.type" placeholder="请选择培训类型" clearable>
          <el-option label="C1自动档" value="C1A" />
          <el-option label="C1手动档" value="C1M" />
          <el-option label="C2手动档" value="C2" />
          <el-option label="B2货车" value="B2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFilter">查询</el-button>
        <el-button @click="resetFilter">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 统计卡片 -->
    <el-row :gutter="20">
      <el-col :span="6" v-for="card in statisticsCards" :key="card.title">
        <el-card class="statistics-card" shadow="hover">
          <div class="card-content">
            <div class="card-icon" :style="{ backgroundColor: card.color }">
              <el-icon>
                <component :is="card.icon" />
              </el-icon>
            </div>
            <div class="card-info">
              <div class="card-title">{{ card.title }}</div>
              <div class="card-value">
                <span class="number">{{ card.value }}</span>
                <span class="unit">{{ card.unit }}</span>
              </div>
              <div class="card-trend">
                <span :class="['trend', card.trend > 0 ? 'up' : 'down']">
                  {{ Math.abs(card.trend) }}%
                  <el-icon>
                    <CaretTop v-if="card.trend > 0" />
                    <CaretBottom v-else />
                  </el-icon>
                </span>
                <span class="trend-text">较上月</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 业务趋势图 -->
      <el-col :span="16">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>业务趋势</span>
              <el-radio-group v-model="trendTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" ref="trendChartRef"></div>
        </el-card>
      </el-col>

      <!-- 培训类型分布 -->
      <el-col :span="8">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>培训类型分布</span>
            </div>
          </template>
          <div class="chart-container" ref="typeChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 考试通过率分析 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>考试通过率分析</span>
              <el-select v-model="passRateSubject" size="small">
                <el-option label="科目一" value="subject1" />
                <el-option label="科目二" value="subject2" />
                <el-option label="科目三" value="subject3" />
                <el-option label="科目四" value="subject4" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="passRateChartRef"></div>
        </el-card>
      </el-col>

      <!-- 教练业绩排行 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>教练业绩排行</span>
              <el-select v-model="performanceMetric" size="small">
                <el-option label="学员数量" value="studentCount" />
                <el-option label="通过率" value="passRate" />
                <el-option label="满意度" value="satisfaction" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="performanceChartRef"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import {
  User,
  Opportunity,
  Medal,
  DataLine,
  CaretTop,
  CaretBottom
} from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 日期快捷选项
const dateShortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    }
  },
  {
    text: '最近一月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    }
  },
  {
    text: '最近三月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    }
  }
]

// 筛选表单
const filterForm = reactive({
  dateRange: [],
  type: ''
})

// 统计卡片数据
const statisticsCards = [
  {
    title: '报名人数',
    value: '156',
    unit: '人',
    icon: User,
    color: '#409EFF',
    trend: 12.5
  },
  {
    title: '考试人次',
    value: '328',
    unit: '人次',
    icon: Opportunity,
    color: '#67C23A',
    trend: 8.2
  },
  {
    title: '通过率',
    value: '92.5',
    unit: '%',
    icon: Medal,
    color: '#E6A23C',
    trend: 3.5
  },
  {
    title: '满意度',
    value: '96.8',
    unit: '%',
    icon: DataLine,
    color: '#F56C6C',
    trend: 1.2
  }
]

// 图表相关
const trendTimeRange = ref('month')
const passRateSubject = ref('subject2')
const performanceMetric = ref('studentCount')

const trendChartRef = ref(null)
const typeChartRef = ref(null)
const passRateChartRef = ref(null)
const performanceChartRef = ref(null)

let trendChart = null
let typeChart = null
let passRateChart = null
let performanceChart = null

// 初始化趋势图
const initTrendChart = () => {
  trendChart = echarts.init(trendChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['报名人数', '考试人次', '通过人数']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
    },
    yAxis: {
      type: 'value',
      name: '人数'
    },
    series: [
      {
        name: '报名人数',
        type: 'line',
        smooth: true,
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: '考试人次',
        type: 'line',
        smooth: true,
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: '通过人数',
        type: 'line',
        smooth: true,
        data: [150, 232, 201, 154, 190, 330, 410]
      }
    ]
  }
  trendChart.setOption(option)
}

// 初始化类型分布图
const initTypeChart = () => {
  typeChart = echarts.init(typeChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '培训类型',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 45, name: 'C1自动档' },
          { value: 25, name: 'C1手动档' },
          { value: 20, name: 'C2手动档' },
          { value: 10, name: 'B2货车' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  typeChart.setOption(option)
}

// 初始化通过率分析图
const initPassRateChart = () => {
  passRateChart = echarts.init(passRateChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['第一次', '第二次', '第三次', '三次以上']
    },
    yAxis: {
      type: 'value',
      name: '通过率（%）',
      max: 100
    },
    series: [
      {
        name: '通过率',
        type: 'bar',
        barWidth: '60%',
        data: [85, 65, 45, 30]
      }
    ]
  }
  passRateChart.setOption(option)
}

// 初始化教练业绩排行图
const initPerformanceChart = () => {
  performanceChart = echarts.init(performanceChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: ['李教练', '王教练', '张教练', '赵教练', '孙教练']
    },
    series: [
      {
        name: '学员数量',
        type: 'bar',
        data: [38, 35, 32, 30, 28]
      }
    ]
  }
  performanceChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  trendChart?.resize()
  typeChart?.resize()
  passRateChart?.resize()
  performanceChart?.resize()
}

// 筛选
const handleFilter = () => {
  // TODO: 实现筛选逻辑
  console.log('筛选条件：', filterForm)
}

// 重置筛选
const resetFilter = () => {
  Object.keys(filterForm).forEach(key => {
    filterForm[key] = ''
  })
  handleFilter()
}

onMounted(() => {
  // 初始化图表
  initTrendChart()
  initTypeChart()
  initPassRateChart()
  initPerformanceChart()

  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)

  // 销毁图表实例
  trendChart?.dispose()
  typeChart?.dispose()
  passRateChart?.dispose()
  performanceChart?.dispose()
})
</script>

<style scoped>
.business-analysis {
  padding: 20px;
}

.filter-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
}

.statistics-card {
  margin-bottom: 20px;
}

.card-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

.card-icon :deep(.el-icon) {
  font-size: 24px;
}

.card-info {
  flex: 1;
}

.card-title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.card-value {
  margin-bottom: 8px;
}

.card-value .number {
  font-size: 24px;
  font-weight: bold;
  margin-right: 4px;
}

.card-value .unit {
  font-size: 14px;
  color: #909399;
}

.card-trend {
  font-size: 12px;
}

.trend {
  display: inline-flex;
  align-items: center;
  margin-right: 4px;
}

.trend.up {
  color: #67c23a;
}

.trend.down {
  color: #f56c6c;
}

.trend-text {
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 320px;
}
</style>